package com.limingfeiyu.sms.common.service.impl.aliyun;

import com.aliyun.tea.utils.StringUtils;
import com.aliyun.teaopenapi.models.OpenApiRequest;
import com.aliyun.teautil.models.RuntimeOptions;
import com.limingfeiyu.sms.common.aliyun.Sample01;
import com.limingfeiyu.sms.common.service.ISmsService;
import com.aliyun.teaopenapi.models.Params;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teaopenapi.Client;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author :   LiMingFeiYu
 * @Date :   2024-04-25 17:28
 * @Explain: SmsService
 */
public class SmsService implements ISmsService {


    private String ALIBABA_CLOUD_ACCESS_KEY_ID = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");

    private String ALIBABA_CLOUD_ACCESS_KEY_SECRET = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

    private String ENDPOINT = "dysmsapi.aliyuncs.com";


    /**
     * 使用AK&SK初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    private Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID)
                .setAccessKeySecret(ALIBABA_CLOUD_ACCESS_KEY_SECRET);
        config.endpoint = ENDPOINT;
        return new com.aliyun.teaopenapi.Client(config);
    }


    /**
     * API 相关
     */
    public static Params createApiInfo() throws Exception {
        Params params = new Params()
                // 接口名称
                .setAction("SendSms")
                // 接口版本
                .setVersion("2017-05-25")
                // 接口协议
                .setProtocol("HTTPS")
                // 接口 HTTP 方法
                .setMethod("POST")
                .setAuthType("AK")
                .setStyle("RPC")
                // 接口 PATH
                .setPathname("/")
                // 接口请求体内容格式
                .setReqBodyType("json")
                // 接口响应体内容格式
                .setBodyType("json");
        return params;
    }

    @Override
    public String SendSms(String phone, String signName, String content, String jsonParam) throws Exception {
        if (StringUtils.isEmpty(phone)) {
            throw new NullPointerException("手机号码不能为空");
        }
        if (StringUtils.isEmpty(signName)) {
            throw new NullPointerException("签名不能为空");
        }
        if (StringUtils.isEmpty(content)) {
            throw new NullPointerException("短信内容不能为空");
        }
        if (StringUtils.isEmpty(jsonParam)) {
            throw new NullPointerException("json参数不能为空");
        }
        // 创建阿里云客户端
        Client client = createClient();
        // 创建API配置信息
        Params params = createApiInfo();
        // query params
        Map<String, Object> queries = new HashMap<>();
        // 手机号码
        queries.put("PhoneNumbers", phone);
        // 签名
        queries.put("SignName", signName);
        // 模版代码
        queries.put("TemplateCode", content);
        // 模版参数
        queries.put("TemplateParam", jsonParam);
        // runtime options
        RuntimeOptions runtime = new RuntimeOptions();
        OpenApiRequest request = new OpenApiRequest()
                .setQuery(com.aliyun.openapiutil.Client.query(queries));
        try {
            String returnStr = client.callApi(params, request, runtime).toString();
            System.out.println("短信返回结果：" + returnStr);
            return returnStr;
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public String SendBatchSms() {
        return null;
    }

    @Override
    public String QuerySendDetails() {
        return null;
    }

    @Override
    public String QuerySendStatistics() {
        return null;
    }
}
